#include "Common.h"
#include <stdio.h>
#include <string.h>

#ifdef QT_WINDOW
#define snprintf _snprintf
#endif

const char *StatusMesg[] = {"Not connected. Please open the connection","Open the success","Open the failure", "Sending data failed"};

void DecToHexDis(const char *DecSource,char *HexDest, int DestSize)
{
    unsigned int i;
    int temp = 0;

    if (!DecSource || !HexDest || DestSize < 0)
            return;

    for (i=0;i<strlen(DecSource);i++)
    {
            temp = strlen(HexDest);
            if (DecSource[i] <= 0xf)
            {
                    snprintf(HexDest+temp,DestSize-temp,"0%x ", DecSource[i]);
            }
            else
            {
                    snprintf(HexDest+temp,DestSize-temp,"%x ", DecSource[i]);
            }
    }

    HexDest[strlen(HexDest)-1] = '\0';

}

int HexToDecDis(const char *HexSource,char *DecDest, int DestSize)
{
    unsigned int i,j,k;
    int count = 0;
    char temp[3] = "";

    j = 0;
    k = 0;

    for (i=0; i<strlen(HexSource); i++)
    {
        if (count == 2)
        {
            temp[j++] = ' ';
            sscanf(temp, "%x ", &DecDest[k++]);
            memset(temp,'\0', sizeof(temp));
            j = 0;
            count = 0;
        }
        if (HexSource[i] != ' ')
        {
            temp[j++] = HexSource[i];
            count++;
        }
        else
        {
            if (count)
            {
                temp[j++] = ' ';
                sscanf(temp, "%x ", &DecDest[k++]);
                memset(temp,'\0', sizeof(temp));
                j = 0;
                count = 0;
            }
            while(HexSource[i] == ' ') i++;
            i--;
        }
    }
    if (strlen(temp))
    {
        sscanf(temp,"%x ", &DecDest[k++]);
    }
    return k;
}

